﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CRMS.Models;
using CRMS.Views.CourseMgnt;
using CRMS.Views.TeacherMgnt;
using CRMS.Views.StudentMgnt;

namespace CRMS.Views.DepartmentMgnt
{
    public partial class frmDepartmentMgnt : Form
    {
        //Field
        private DepartmentRep departRep = new DepartmentRep();
        //Constructor
        public frmDepartmentMgnt()
        {
            InitializeComponent();
        }
        //Event handlers
        //选择在结果中搜索事件
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                TstripCboxCondition.Enabled = false;
            }
            else
            {
                TstripCboxCondition.Enabled = true;
            }
        }

        private void TstripBtnAdd_Click(object sender, EventArgs e)
        {
            MessageBox.Show("请在文本框中填写信息！");
            txtDepID.ReadOnly = false;
            txtDepName.ReadOnly = false;
            txtDepID.Text = String.Empty;
            txtDepName.Text = String.Empty;
            txtDepID.Focus();
        }

        private void TstripModify_Click(object sender, EventArgs e)
        {
            txtDepID.ReadOnly = false;
            txtDepName.ReadOnly = false;
            this.TstripBtnSave.Click -= new System.EventHandler(TstripBtnSave_Click);
            this.TstripBtnSave.Click += new System.EventHandler(this.TstripBtnSave_Change);
        }

        private void TstripBtnSave_Change(object sender, EventArgs e)
        {
            Department department = new Department();
            department = departRep.GetDeptByID(txtDepID.Text.Trim());
            if (txtDepID.Text.Trim() == String.Empty || txtDepName.Text.Trim() == String.Empty)
            {
                MessageBox.Show("必须填写全部信息！");
            }
            else
            {
                department.DepartmentId = txtDepID.Text.Trim();
                department.DepartmentName = txtDepName.Text.Trim();
                departRep.Save();
            }
        }

        private void TstripBtnSave_Click(object sender, EventArgs e)
        {
            Department department = new Department();
            if (txtDepID.Text.Trim() == String.Empty || txtDepName.Text.Trim() == String.Empty)
            {
                MessageBox.Show("必须填写全部信息！");
            }
            else
            {
                if (departRep.GetDeptByID(txtDepID.Text.Trim()) != null)
                {
                    MessageBox.Show("该院系已经存在！");
                }
                else
                {
                    department.DepartmentId = txtDepID.Text.Trim();
                    department.DepartmentName = txtDepName.Text.Trim();
                    departRep.Add(department);
                    departRep.Save();
                    MessageBox.Show("添加成功！");
                    frmDepartmentMgnt_Load(sender, e);
                }
            }
        }
                
        private void frmDepartmentMgnt_Load(object sender,EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = departRep.GetDepartments();
        }

        private void TstripBtnDelete_Click(object sender, EventArgs e)
        {
            string deleteID = dataGridView1[0, dataGridView1.CurrentCellAddress.Y].Value.ToString();
            Department department = departRep.GetDeptByID(deleteID);
            if (department != null)
            {
                departRep.Delete(department);
                departRep.Save();
                MessageBox.Show("已成功删除！");
                frmDepartmentMgnt_Load(sender, e);
            }
            else
            {
                MessageBox.Show("该院系不存在或者已经被删除！");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value == null)
            {
                return;
            }
            else
            {
                txtDepID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtDepName.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtDepID.ReadOnly = true;
                txtDepName.ReadOnly = true;
            }
        }

        private void TstripBtnLookUp_Click(object sender, EventArgs e)
        {
            List<Department> objs = new List<Department>();
            if (this.TstripCboxCondition.Text == "院系名称")
            {
                objs.Add(departRep.GetDeptByName(this.TstripTxtKeyword.Text.Trim()));
            }
            else
            {
                objs.Add(departRep.GetDeptByID(this.TstripTxtKeyword.Text.Trim()));
            }
            this.dataGridView1.DataSource = objs;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (frmMain.fm.CheckForm("frmCourseMgnt") == true)
            {
                return;
            }
            else
            {
                string departmentId = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                frmCourseMgnt fcm = new frmCourseMgnt(departmentId);
                fcm.MdiParent = frmMain.fm;
                this.Close();
                fcm.Show();
            }
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (frmMain.fm.CheckForm("frmTeacherInfo") == true)
            {
                return;
            }
            else
            {
                string departmentId = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                frmTeacherInfo ft = new frmTeacherInfo(String.Empty,departmentId);
                ft.MdiParent = frmMain.fm;
                this.Close();
                ft.Show();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (frmMain.fm.CheckForm("frmStudentMgnt") == true)
            {
                return;
            }
            else
            {
                string departmentId = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                frmStudentMgnt fs = new frmStudentMgnt(departmentId);
                fs.MdiParent = frmMain.fm;
                this.Close();
                fs.Show();
            }
        }
   }
}
